package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.BringTheOrderMapper;
import com.gxa.insurance.mapper.ImagesFileMapper;
import com.gxa.insurance.mapper.OrderMapper;
import com.gxa.insurance.pojo.BringTheOrder;
import com.gxa.insurance.service.BringTheOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @date 2022/6/7
 */
@Service("bringTheOrderService")
public class BringTheOrderServiceImpl implements BringTheOrderService {

    @Autowired
    BringTheOrderMapper bringTheOrderMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ImagesFileMapper imagesFileMapper;

    @Override
    public Integer add(BringTheOrder bringTheOrder) {
//        return bringTheOrderMapper.add(bringTheOrder);
        return null;
    }

    @Transactional()
    @Override
    public Integer update(Map map) throws Exception {
            Object orderId = map.get("orderId");
            Integer id = Integer.valueOf(String.valueOf(orderId));
            String orderStatus = (String)map.get("orderStatus");
            List<Integer> status = orderMapper.selectStatus(orderStatus);
            Integer statusId = status.get(0);
            Integer integer = orderMapper.updateOrderStatus(statusId,id);
            if(integer==0){
                throw new BusinessException(ExceptionCode.UPDATE_FILED);
            }
            String images =(String) map.get("photoAttachments");
            String[] split = images.split(",");
            for (int i = 0; i < split.length; i++) {
                Integer insert = imagesFileMapper.insert(id, split[i]);
            }

            String isMake = (String)map.get("isMakeCompensation");
            Boolean isMakeCompensation = Boolean.valueOf(isMake);
            map.put("isMakeCompensation",isMakeCompensation);
            String isSend = (String)map.get("isSendMessage");
            Boolean isSendMessage = Boolean.valueOf(isSend);
            map.put("isSendMessage",isSendMessage);
            Integer rows = bringTheOrderMapper.update(map);
            if(rows==0){
                throw new BusinessException(ExceptionCode.UPDATE_FILED);
            }
            return rows;
    }
}
